package com.qtt.app.controller;

import com.qtt.app.common.ApiResponse;
import com.qtt.app.entity.Teachers;
import com.qtt.app.entity.ReturnInfo.ReturnTeacherInfo;
import com.qtt.app.service.AppTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.qtt.app.entity.RequestData.*;

import java.util.List;

@Api(tags = "讲师管理", description = "TeacherController")   //tags是api名字，description是简介，value:已废用
@RestController
@RequestMapping("/Teacher")
public class AppTeacherController {
    @Autowired
    AppTeacherService appTeacherService;

    @ApiOperation(value = "通过机构id查看该机构的所有讲师(机构详情页)", notes = "")  //value是Get /Msg/list旁边的那个api简介；notes是点开的介绍，可用于详细说明api作用
    @GetMapping("/InstTeacherList")
    public ApiResponse<List<ReturnTeacherInfo>> getInstTeacherByInstId(@ApiParam(value="机构id",required = true) @RequestParam(value="institutionId",required=true) Integer institutionId){
        List<ReturnTeacherInfo> entities = appTeacherService.getInstitutionTeacherByInstId(institutionId);
        if(entities.size()==0){return ApiResponse.<List<ReturnTeacherInfo>>builder().code(400).message("该机构还没有讲师").data(entities).build();}
        return ApiResponse.<List<ReturnTeacherInfo>>builder().code(200).message("查看成功").data(entities).build();
    }

    @ApiOperation(value = "添加一条新的记录", notes = "")
    @PostMapping("/list")
    public ApiResponse<String> addMsg(@ApiParam(value="讲师信息：teacherName讲师名字;teacherPicture讲师图片; introduction讲师介绍;teacherRank讲师职称;teacherGrade讲师评分(0-5,float);teachNum教过的学生数量;courseNum拥有的课程数量;institutionId机构id",required = true) @RequestBody(required=true) AddTeacher requestData){
        //判断，如果传入的是默认值，需要设置为null或者默认图片
        Teachers teachersEntity = new Teachers();
        teachersEntity.setRealName(requestData.getTeacherName());
        teachersEntity.setRanks(requestData.getTeacherRank());
        teachersEntity.setHead(requestData.getTeacherPicture());
        teachersEntity.setIntroduction(requestData.getIntroduction());
        teachersEntity.setGrade(requestData.getTeacherGrade());
        teachersEntity.setCourseNum(requestData.getCourseNum());
        teachersEntity.setTeachNum(requestData.getTeachNum());
        int LastTeacherId = appTeacherService.addTeacher(teachersEntity);
        System.out.println("lastId="+LastTeacherId);
        if (LastTeacherId != 0) {
            int flag = appTeacherService.addAppTeacher(LastTeacherId, requestData.getInstitutionId());
            if (flag == 200) {
                return ApiResponse.<String>builder().code(200).message("添加成功").data("success").build();
            } else {
                return ApiResponse.<String>builder().code(400).message("往app_teacher表添加数据失败").data("fail").build();
            }
        }
        else {
            return ApiResponse.<String>builder().code(400).message("往teacher表添加数据失败").data("fail").build();
        }
    }

//    @ApiOperation(value = "添加一条新的记录", notes = "")
//    @PostMapping("/list")
//    public ApiResponse<String> addMsg(@ApiParam(value="讲师名字",required = true) @RequestParam(value="teacherName",required=true) String teacherName,
//                                      @ApiParam(value="讲师图片",required = true) @RequestParam(value="teacherPicture",required=true) String teacherPicture,
//                                      @ApiParam(value="讲师介绍",required = true) @RequestParam(value="introduction",required=true) String introduction,
//                                      @ApiParam(value="讲师职称",required = true) @RequestParam(value="teacherRank",required=true) String teacherRank,
//                                      @ApiParam(value="讲师评分(0-5,float)",required = true) @RequestParam(value="teacherGrade",required=true) Float teacherGrade,
//                                      @ApiParam(value="拥有的课程数量",required = true) @RequestParam(value="courseNum",required=true) Integer courseNum,
//                                      @ApiParam(value="教过的学生数量",required = true) @RequestParam(value="teacherNum",required=true) Integer teacherNum,
//                                      @ApiParam(value="机构id",required = true) @RequestParam(value="institutionId",required=true) Integer institutionId){
//        //判断，如果传入的是默认值，需要设置为null或者默认图片
//        Teachers teachersEntity = new Teachers();
//        teachersEntity.setRealName(teacherName);
//        teachersEntity.setRanks(teacherRank);
//        teachersEntity.setHead(teacherPicture);
//        teachersEntity.setIntroduction(introduction);
//        teachersEntity.setGrade(teacherGrade);
//        teachersEntity.setCourseNum(courseNum);
//        teachersEntity.setTeachNum(teacherNum);
//        int LastTeacherId = appTeacherService.addTeacher(teachersEntity);
//        System.out.println("lastId="+LastTeacherId);
//        if (LastTeacherId != 0) {
//            int flag = appTeacherService.addAppTeacher(LastTeacherId, institutionId);
//            if (flag == 200) {
//                return ApiResponse.<String>builder().code(200).message("添加成功").data("success").build();
//            } else {
//                return ApiResponse.<String>builder().code(400).message("往app_teacher表添加数据失败").data("fail").build();
//            }
//        }
//        else {
//            return ApiResponse.<String>builder().code(400).message("往teacher表添加数据失败").data("fail").build();
//        }
//    }

//    @ApiOperation(value = "课程标签页的推荐讲师，按照评分返回()", notes = "")  //value是Get /Msg/list旁边的那个api简介；notes是点开的介绍，可用于详细说明api作用
//    @GetMapping("/RecommendTeachers")
//    public ApiResponse<List<Teachers>> getRecommendTeachers(){
//        List<Teachers> entities = appTeacherService.getRecommendTeachers();
//        if(entities.size()==0){return ApiResponse.<List<Teachers>>builder().code(400).message("该机构还没有讲师").data(entities).build();}
//        return ApiResponse.<List<Teachers>>builder().code(200).message("查看成功").data(entities).build();
//    }
}
